package com.atmaowei.controller;

import com.atmaowei.common.Result;
import com.atmaowei.controller.request.CoursePageRequest;
import com.atmaowei.controller.request.StudentPageRequest;
import com.atmaowei.entity.Course;
import com.atmaowei.entity.Department;
import com.atmaowei.entity.Student;
import com.atmaowei.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;


    @PostMapping("/save")
    public Result save(@RequestBody Course course) {
        courseService.save(course);
        return Result.success();
    }

    @DeleteMapping("/delete/{cno}")
    public Result delete(@PathVariable String cno) {
        courseService.delete(cno);
        return Result.success();
    }

    @GetMapping("/getCnoByCname/{cname}")
    public Result getCnoByCname(@PathVariable String cname) {
        return Result.success(courseService.getCnoByCname(cname));
    }

    @PutMapping("/update/{oldCno}")
    public Result update(@RequestBody Course course, @PathVariable String oldCno) {
        courseService.update(course, oldCno);
        return Result.success();
    }

    @GetMapping("/{cno}")
    public Result getByCno(@PathVariable String cno) {
        Course course = courseService.getByCno(cno);
        return Result.success(course);
    }

    @GetMapping("/getAllCourse")
    public Result getAllCourse() {
        List<Course> courses = courseService.getAllCourse();
        return Result.success(courses);
    }

    @GetMapping("/page")
    public Result page(CoursePageRequest coursePageRequest) {
        return Result.success(courseService.page(coursePageRequest));
    }

    @PostMapping("/deleteSelected")
    public Result deleteSelected(@RequestBody List<Course> courses) {
        courseService.deleteSelected(courses);
        return Result.success();
    }
}
